﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhaseUatt2
{
    class regulatorConditions
    {
        public double m; //масса шаров центробежного регулятора// mass of efferent regulator orb
        public double F; //суммарная нагрузка на вал паровой машины//total load on steam-engine axle
        public double g; //ускорение свободного падения// acceleration of gravity
        public double J; //момент инерции маховика паровой машины//moment of inertia steem-engine flywheel
        public double n; //передаточное число//gear-ratio
        public double b; //коэффициент трения//constant of friction
        public double k; //константа регуляции//regulation constant

        public regulatorConditions()
        {
            m = 1;
            F = 1;
            g = 10;
            J = 1;
            n = 0.5;
            b = 0.25;
            k = 1;
        }
        public regulatorConditions(double m, double F, double g, double J, double n, double b, double k)
        {
            this.m = m;
            this.F = F;
            this.g = g;
            this.J = J;
            this.n = n;
            this.b = b;
            this.k = k;
        }

        public double[] uattEq(double w, double fi, double dfi)  //вщзвращает массив производных w, fi, dfi
        {
            double[] res = new double[3];

            res[0] = k * Math.Cos(fi) - F;
            res[1] = dfi;
            res[2] = m * n * n * w * w * Math.Sin(fi) * Math.Cos(fi) - m * 10 * Math.Sin(fi) - b * dfi;

            return res;
        }

        public double[] RungeKutt4uatt(double h, double w0, double dfi0, double fi0) // w, dfi, fi
        {
            double[] res = new double[3];

            double[] k1 = new double[3];
            k1[0] = h * uattEq(w0, fi0, dfi0)[0];
            k1[1] = h * uattEq(w0, fi0, dfi0)[1];
            k1[2] = h * uattEq(w0, fi0, dfi0)[2];

            double[] k2 = new double[3];
            k2[0] = h * uattEq(w0 + 0.5 * k1[0], fi0 + 0.5 * k1[2], dfi0 + 0.5 * k1[1])[0];
            k2[1] = h * uattEq(w0 + 0.5 * k1[0], fi0 + 0.5 * k1[2], dfi0 + 0.5 * k1[1])[1];
            k2[2] = h * uattEq(w0 + 0.5 * k1[0], fi0 + 0.5 * k1[2], dfi0 + 0.5 * k1[1])[2];

            double[] k3 = new double[3];
            k3[0] = h * uattEq(w0 + 0.5 * k2[0], fi0 + 0.5 * k2[2], dfi0 + 0.5 * k2[1])[0];
            k3[1] = h * uattEq(w0 + 0.5 * k2[0], fi0 + 0.5 * k2[2], dfi0 + 0.5 * k2[1])[1];
            k3[2] = h * uattEq(w0 + 0.5 * k2[0], fi0 + 0.5 * k2[2], dfi0 + 0.5 * k2[1])[2];

            double[] k4 = new double[3];
            k4[0] = h * uattEq(w0 + k3[0], fi0 + k3[2], dfi0 + k3[1])[0];
            k4[1] = h * uattEq(w0 + k3[0], fi0 + k3[2], dfi0 + k3[1])[1];
            k4[2] = h * uattEq(w0 + k3[0], fi0 + k3[2], dfi0 + k3[1])[2];

            res[0] = w0 + (1.0 / 6.0) * (k1[0] + 2.0 * k2[0] + 2.0 * k3[0] + k4[0]);
            res[1] = dfi0 + (1.0 / 6.0) * (k1[1] + 2.0 * k2[1] + 2.0 * k3[1] + k4[1]);
            res[2] = fi0 + (1.0 / 6.0) * (k1[2] + 2.0 * k2[2] + 2.0 * k3[2] + k4[2]);

            return res;
        }

    }
}
